<?php

namespace App\Admin\Controllers;

use App\Models\Fire;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Row;

class FireController extends Controller
{
    use ModelForm;

    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $game = Fire::find($id);
            $counter = $game->counter;
            if(!$counter){
                $content->header('暂无统计数据');
                return $content->render();
            }
            $decode_counter = json_decode($counter, true);
            $sum = 0;
            foreach ($decode_counter as $value)
            {
                $sum += $value;
            }
            $correct_count = $game->correct_count;
            $title = $game->title;
            $answers = $game->answers;
            $arr = explode("|", $answers);
            $answer = json_encode($arr);
            $name = "fire";
            $content->header($title.'的统计信息');
            $content->description('.....');

            $content->row(function (Row $row) use ($counter, $answer, $sum, $correct_count, $name){
                $row->column(6,view('admin.charts.bar',compact(['counter', 'answer'])));
                $row->column(6,view('admin.charts.doughnut',compact(['sum','correct_count', 'name'])));
            });

            $content->render();
        });
    }
    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('火灾体验题目列表');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑题目');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('新增题目');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Fire::class, function (Grid $grid) {
            $grid->disableFilter();
            $grid->disableExport();
            $grid->disableCreateButton();
            $grid->disableRowSelector();
            $grid->tools(function ($tools) {
                $tools->append('<a href="./statistic?type=fire"><button class="btn btn-warning btn-sm">排行信息</button></a>');
            });
            $grid->actions(function ($actions) {
                $actions->disableDelete();
                $actions->disableEdit();
                $id = $actions->getKey();
                $actions->append('<a href="./fire/'.$id.'"><button class="btn btn-info btn-sm">统计信息</button></a>');
            });

            $grid->id('ID')->sortable();
            $grid->column('title', '题目');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Fire::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('title', '题目名称');
            $form->text('answers', '选项');
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
